package jdk5.concurrent.tiger;

import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.Executor;

import jdk5.concurrent.Worker;

public class FileServer extends Thread {
	private int port;
	private Executor pool;

	public FileServer(int port) {
		this.port = port;
		pool = Executors.newFixedThreadPool(5);
	}

	public void run() {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(port);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
		while (true) {
			try {
				Socket s = ss.accept();
				Runnable r = new Worker(s);
				pool.execute(r);
			} catch (IOException e) {
				if (ss.isClosed())
					break;
				e.printStackTrace();
			}
		}
	}

}
